package com.sz.businessrecommend.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.sz.businessrecommend.bmo.IBusinessRecommendBMO;
import com.sz.common.ICommonDAO;

public class OpreateBusinessRecommend extends HttpServlet {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	protected final Log logger = LogFactory.getLog(getClass());

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		doPost(request, response);
		
	}
	
	@SuppressWarnings("unchecked")
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String type = request.getParameter("OPERATE_TYPE");
		
		if(type.compareToIgnoreCase("DEL") == 0) {
			delBusinessRecommend(request, response);
		}
		else if(type.compareToIgnoreCase("ADD") == 0)
		{
			addBusinessRecommend(request, response);
		}

	}
	
	@SuppressWarnings("unchecked")
	private void delBusinessRecommend(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException
	{
		try
		{
			String business_id = request.getParameter("BUSINESS_ID");
			

			Map map = new HashMap();
			map.put("BUSINESS_RECOMMEND_ID", business_id);
			

			ServletContext servletContext = request.getSession().getServletContext();
			ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
			IBusinessRecommendBMO bmo = (IBusinessRecommendBMO) context.getBean("BusinessRecommendBMO");
			long delflag = bmo.delRecommend(map);
			
			String msg = "";
			if (delflag == 1)
			{
				msg = "成功";
			}
			else
			{
			    msg = "失败";
			}
		
			response.setCharacterEncoding("utf-8");
			response.getWriter().print(msg);
		} 
		catch (Exception e)
		{
			logger.error(e.getMessage());
			throw new ServletException(e.getMessage());
		}
	}
	
	@SuppressWarnings("unchecked")
	private void addBusinessRecommend(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException
	{
		try
		{
			ServletContext servletContext = request.getSession().getServletContext();
			ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
			IBusinessRecommendBMO bmo = (IBusinessRecommendBMO) context.getBean("BusinessRecommendBMO");
			
			
			Map map = new HashMap();
			
			String Primary_Business_ID = request.getParameter("PRIMARY_BUSINESS_ID");
			String Business_Recommend_ID = request.getParameter("BUSINESS_RECOMMEND_ID");		


			map.put("PRIMARY_ID", Primary_Business_ID);
			map.put("RECOMMEND_ID", Business_Recommend_ID);
			
			long delflag = bmo.addRecommend(map);


			
			String msg = "";
			if (delflag == 1)
			{
				msg = "成功";
			}
			else
			{
			    msg = "失败";
			}
		
			response.setCharacterEncoding("utf-8");
			response.getWriter().print(msg);
		} 
		catch (Exception e)
		{
			logger.error(e.getMessage());
			throw new ServletException(e.getMessage());
		}
	}

}
